<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <form class="layui-form" lay-filter="myform" id="myform">
                    <input type="reset" id="btReset" class="layui-hide" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">题干</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">题型</label>
                        <div class="layui-input-inline">
                            <select name="questionType" id="questionType"></select>
                        </div>
                        <label class="layui-form-label">中/英文</label>
                        <div class="layui-input-inline">
                            <select name="lg" id="lg"></select>
                        </div>
                        <label class="layui-form-label">难度</label>
                        <div class="layui-input-inline">
                            <select name="difficulty" id="difficulty"></select>
                        </div>
                        <label class="layui-form-label">试题来源</label>
                        <div class="layui-input-inline">
                            <select name="source" id="source"></select>
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">篇</label>
                        <div class="layui-input-inline">
                            <select name="pianId" id="pianId" lay-filter="pianId"></select>
                        </div>
                        <label class="layui-form-label">章</label>
                        <div class="layui-input-inline">
                            <select name="chapterId" id="chapterId" lay-filter="chapterId"></select>
                        </div>
                        <label class="layui-form-label">节</label>
                        <div class="layui-input-inline">
                            <select name="jieId" id="jieId"></select>
                        </div>
                        <label class="layui-form-label">试题类型</label>
                        <div class="layui-input-inline">
                            <select name="contentType" id="contentType"></select>
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">排除</label>
                        <div class="layui-input-inline" style="width:1130px!important">
                            <input type="text" name="papername" id="papername" autocomplete="off" onclick="selectPapers()" class="layui-input">
                            <input type="hidden" name="paperid" id="paperid" class="layui-input">
                            <!--<div id="epaper" class="xm-select-demo" style="z-index:999"></div>-->
                        </div>
                    </div>
                    <div class="layui-form-item" style="text-align: center">
                        <div class="layui-inline" style="margin-left: 5px;">
                            <div class="layui-btn-group">
                                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-row layui-col-space5">
                <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
            </div>
        </div>
    </div>
</div>
<button type="button" id="importWord" style="display: none">导入</button>
<script th:inline="none"></script>
<script src="layuiadmin/plugins/xm-select/xm-select.js"></script>
<script>
    let joinid = `[[${joinid}]]`
    if(!joinid){
        joinid = snowId();//生成组卷唯一ID
    }
    // var epaper = xmselect('paper/getPageList?limit=30&page=1' , 'epaper' ,{key:"id",text:"name"});
    layui.use(['table','form','upload'], function(){
        var table = layui.table,
            form = layui.form;
        createSelect("paper-lg","lg",form,'','请选择');
        createSelect("question-source","source",form,'','请选择');
        createSelect("question-difficulty","difficulty",form,'','请选择');
        createSelect("paper-type","contentType",form,'','请选择');
        createSelect("question-type","questionType",form,'','请选择');
        select('pian/getPageList?limit=30&page=1' , 'pianId',form,'name','id')
        select('chapter/getPageList?limit=0&page=1', 'chapterId',form,'name','id')
        select('jie/getPageList?limit=0&page=1' , 'jieId',form,'name','id')
        form.on("select(pianId)",function(data){
            select('chapter/getPageList?limit=50&page=1&pianId='+data.value , 'chapterId',form,'name','id')
            form.on("select(chapterId)",function(data){
                select('jie/getPageList?limit=50&page=1&chapterId='+data.value , 'jieId',form,'name','id')
            })
            select('jie/getPageList?limit=0&page=1' , 'jieId',form,'name','id')
        })
        table.render({
            elem: '#main-table'
            ,height: 550
            ,url: 'libQuestion/getPageList'
            ,title: '题目列表'
            ,toolbar: '#toolbar'
            ,limit:20
            ,page: true
            ,cols: [
                [
                    {type: 'checkbox'},
                    {field: 'code', title: "编号",width:90}
                    ,{field: 'text2', title: "题干",width:320}
                    ,{field: 'questionTypeTxt',title: "题型",width:100}
                    ,{field: 'pianName', title: "篇",width:180}
                    ,{field: 'chapterName', title: "章",width:180}
                    ,{field: 'jieName', title: "节",width:250}
                    ,{field: 'knowledgeName',title: "知识点",width:250}
                    ,{field: 'createTime',title: "登记时间",width:170}
                    ,{field: 'difficultyTxt',title: "难度",width:90}

                    ,{field: 'contentTypeTxt',title: "试题类型",width:120}
                    ,{field: 'paperTypeTxt',title: "试卷类型",width:120}
                    ,{field: 'lgTxt',title: "语言",width:90}
                ]
            ]
            ,done:function(){
                enableRowClick(table,form,'main-table',"main-table");
            }
        });
        //头工具栏事件
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'view':

                    top.layer.open({
                        id:"view",
                        type: 2,
                        title:'查看选择结果',
                        area:['1450px','860px'],
                        content:  'rooter?url=ksxt/paper/view-result&joinid='+joinid,
                        btn:['组卷','关闭'],
                        yes:function(index,layero){
                            var dataForm = top.layer.getChildFrame('form', index);
                            let name = dataForm.find("input[name=name]").val();
                            let collegeId = dataForm.find(":input[name=collegeId]").val();
                            let categoryId = dataForm.find(":input[name=categoryId]").val();//考试科目
                            let grade = dataForm.find(":input[name=grade]").val();
                            let gradeYear = dataForm.find(":input[name=gradeYear]").val();
                            let semester = dataForm.find(":input[name=semester]").val();
                            let testType = dataForm.find(":input[name=testType]").val();
                            let howlong = dataForm.find(":input[name=howlong]").val();
                            let paperType = dataForm.find(":input[name=paperType]").val();
                            let studentType = dataForm.find(":input[name=studentType]").val();
                            let testPhase = dataForm.find(":input[name=testPhase]").val();
                            let major = dataForm.find(":input[name=majorId]").val();
                            let score1 = dataForm.find(":input[name=score1]").val();
                            let score2 = dataForm.find(":input[name=score2]").val();
                            let score3 = dataForm.find(":input[name=score3]").val();
                            let score4 = dataForm.find(":input[name=score4]").val();
                            let score5 = dataForm.find(":input[name=score5]").val();
                            let lg = dataForm.find(":input[name=lg]").val();
                            let values = {howlong:howlong,name:name,collegeId:collegeId,categoryId:categoryId,studentType:studentType,paperType:paperType
                                ,gradeYear:gradeYear,semester:semester,testType:testType,joinid:joinid+"",testPhase:testPhase,grade:grade,major:major,lg:lg
                            ,score1:score1,score2:score2,score3:score3,score4:score4,score5:score5};
                            axios.post("paper/generate2",values).then(res=>{
                                if(res.data.code==1){
                                    top.layer.msg(res.data.msg);
                                }else{
                                    let fr = top.$("iframe[src*=paper-list]");
                                    fr[0].contentWindow.loadGrid('main-table')
                                    top.layer.msg("组卷成功");
                                    top.layer.closeAll();
                                }


                            })
                            return false
                        }
                    });
                    break;
                case 'select':
                    top.layer.open({
                        id:"view",
                        type: 2,
                        title:'选取试题',
                        area:['1700px','700px'],
                        content:  'rooter?url=ksxt/paper/select-question-num&joinid='+joinid,
                        btn:['加入队列','关闭'],
                        yes:function(index,layero){
                            var iframeWin = top.window[layero.find('iframe')[0]['name']];
                            let joinid2 = iframeWin.joinid2;
                            axios.get("libQuestion/addJoined",{params:{joinid:joinid,joinid2:joinid2}}).then(res=>{
                                if(res.data.code==0){
                                    top.layer.msg(res.data.msg,{time:5000},function (){
                                        top.layer.close(index);
                                    });
                                    top.layer.alert(res.data.msg,{icon:1});
                                }else {
                                    top.layer.alert(res.data.msg,{icon:2});
                                }

                            })
                            return false
                        }
                    });
                    break;
                case 'select1':
                    var data = checkStatus.data;
                    if(data.length===0){
                        layer.msg("请选择题目!")
                        return false;
                    }
                    var ids = [];
                    for(var i=0;i<data.length;i++){
                        ids.push(data[i].id);
                    }
                    axios.get("libQuestion/addJoined2",{params:{joinid:joinid,id:ids.join(",")}}).then(res=>{
                        if(res.data.code==0){
                            top.layer.msg(res.data.msg,{time:5000},function (){
                                top.layer.close(index);
                            });
                            top.layer.alert(res.data.msg,{icon:1});
                        }else {
                            top.layer.alert(res.data.msg,{icon:2});
                        }

                    })
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;
            };
        });
        var $ = layui.$, active = {
            query: function(){
                // let exps = epaper.getValue();
                // var excludePaperIds = exps.map(x=>x.value);
                let excludePaperIds = $("#paperid").val();
                table.reload("main-table",{where:{excludePaperIds:excludePaperIds,name:$("#name").val(),difficulty:$("#difficulty").val()
                        ,lg:$("#lg").val(),questionType:$("#questionType").val(),contentType:$("#contentType").val()
                        ,source:$("#source").val(),pianId:$("#pianId").val(),chapterId:$("#chapterId").val(),jieId:$("#jieId").val()}})
            },
            resetBtn:function () {
                $("#btReset").click();

            }
        };
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
    function edit(id){
        top.layer.open({
            id:"editwin",
            type: 2,
            title:'修改题目',
            area:['1500px','880px'],
            content:  'rooter?url=ksxt/lib/add-question1&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = top.layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }
    function selectPapers(){
        top.layer.open({
            id:"editwin",
            type: 2,
            title:'选择试卷',
            area:['1200px','780px'],
            content:  'rooter?url=ksxt/paper/selectEpaper&id='+1,
            btn:['选择','关闭'],
            yes:function(index,layero){
                var iframeWin = top[layero.find('iframe')[0]['name']];
                let idnames = iframeWin.getIds();
                let ids = [],names = [];
                for(var i=0;i<idnames.length;i++){
                    ids.push(idnames[i].id);
                    names.push(idnames[i].name);
                }
                $('#paperid').val(ids.join(","));
                $('#papername').val(names.join(","));
                top.layer.close(index)
            }
        });
    }
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="select1"><i class="layui-icon layui-icon-edit"></i>选取</button>
        <button class="layui-btn layui-btn-sm" lay-event="select"><i class="layui-icon layui-icon-edit"></i>设置选取数量</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="view"><i class="layui-icon layui-icon-find-fill"></i>查看选择结果</button>
    </div>
</script>
</body>
</html>